SocketCluster JavaScript client
Client module for SocketCluster.
Setting up
You will need to install both socketcluster-client
and socketcluster-server
(https://github.com/SocketCluster/socketcluster-server).
To install this module:
npm install socketcluster-client
How to use
The socketcluster-client script is called socketcluster-client.js
(located in the main socketcluster-client directory).
Embed it in your HTML page like this:
<script type="text/javascript" src="/socketcluster-client.js"></script>
* Note that the src attribute may be different depending on how you setup your HTTP server.
Once you have embedded the client socketcluster-client.js
into your page, you will gain access to a global socketClusterClient
object.
You may also use CommonJS require
or ES6 module imports.
Connect to a server
let socket = socketClusterClient.create({
hostname: 'localhost',
port: 8000
});
Transmit data
socket.transmit('foo', 123);
Invoke an RPC
(async () => {
let result = await socket.invoke('myProc', 123);
})();
Subscribe to a channel
(async () => {
let myChannel = socket.subscribe('myChannel');
await myChannel.listener('subscribe').once();
})();
Get a channel without subscribing
(async () => {
let myChannel = socket.channel('myChannel');
myChannel.subscribe();
await myChannel.listener('subscribe').once();
})();
Publish data to a channel
myChannel.transmitPublish('This is a message');
socket.transmitPublish('myChannel', 'This is a message');
(async () => {
try {
await myChannel.invokePublish('This is a message');
} catch (error) {
}
try {
await socket.invokePublish('myChannel', 'This is a message');
} catch (error) {
}
})();
Consume data from a channel
(async () => {
for await (let data of myChannel) {
}
})();
Connect over HTTPS:
let options = {
hostname: 'securedomain.com',
secure: true,
port: 443,
rejectUnauthorized: false
};
let socket = socketClusterClient.create(options);
For more detailed examples of how to use SocketCluster, see test/integration.js
.
Also, see tests from the socketcluster-server
module.
Connect Options
See all available options: https://socketcluster.io/
let options = {
path: '/socketcluster/',
port: 8000,
hostname: '127.0.0.1',
autoConnect: true,
secure: false,
rejectUnauthorized: false,
connectTimeout: 10000,
ackTimeout: 10000,
channelPrefix: null,
disconnectOnUnload: true,
autoReconnectOptions: {
initialDelay: 10000,
randomness: 10000,
multiplier: 1.5,
maxDelay: 60000
},
authEngine: null,
codecEngine: null,
subscriptionRetryOptions: {},
query: {
yourparam: 'hello'
}
};
Running the tests
- Clone this repo:
git clone git@github.com:SocketCluster/socketcluster-client.git
- Navigate to project directory:
cd socketcluster-client
- Install all dependencies:
npm install
- Run the tests:
npm test
Compatibility mode
For compatibility with an existing SocketCluster server, set the protocolVersion
to 1
and make sure that the path
matches your old server path:
let socket = socketClusterClient.create({
protocolVersion: 1,
path: '/socketcluster/'
});
Developing
Install all dependencies
cd socketcluster-client
npm install -g gulp gulp-cli browserify uglify-es
npm install
Building
To build the SocketCluster client:
npm run build
Change log
See the 'releases' section for changes: https://github.com/SocketCluster/socketcluster-client/releases
License
(The MIT License)
Copyright (c) 2013-2023 SocketCluster.io
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.